< previous page page_217 next page >

Page 217
Once the program knows the angle q and the distance R, it can find the new point's position (x', y') using these equations:
e10a6a0a59f035e870d90cca05b85fce.gif
x' = R * Cos(q + f)
y' = R * Sin(
q + f)
Now suppose the original image has center (CxIn, CyIn) and the result picture has center (CxOut, CyOut). The program can rotate the point around (CxIn, CyIn) by first subtracting CxIn and CyIn from the point's coordinates. It can then center the pixel in the output picture by adding CxOut and CyOut to the results.
Inverting these steps to find the inverse mapping would be difficult. Fortunately, the inverse of a rotation through the angle q is simply a rotation through the angle q. To find the inverse operation, you use the same steps with the roles of the input and output pixels switched.
Program Trans uses the MapPixel subroutine shown in the following code to map output pixels back to input positions. First it subtracts CxOut and CyOut from the output pixel's coordinates. This transforms the rotation into a rotation around the origin.
The routine calculates the distance R from the translated point to the origin. It then uses the ATan2 function to find the angle theta to the output point. MapPixel adds the angle of rotation phi to theta and uses the value R to find the input pixel's coordinates with respect to the origin. Finally, it adds the values CxIn and CyIn to center the result in the input image.
e10a6a0a59f035e870d90cca05b85fce.gif
' Map the output pixel (ix_out, iy_out) to the input
' pixel (x_in, y_in).
Private Sub MapPixel( _
    ByVal ix_out As Single, _
    ByVal iy_out As Single, _
    ByRef x_in As Single, ByRef y_in As Single)
Dim dx As Single
Dim dy As Single
Dim R As Single
Dim theta As Single

    dx = ix_out - CxOut
    dy = iy_out - CyOut
    R = Sqr(dx * dx + dy * dy)
    theta = ATan2(dy, dx)
    x_in = CxIn + R * Cos(theta + Phi)
    y_in = CyIn + R * Sin(theta + Phi)
End Sub
There is one other detail you need to consider when rotating an image: The size of the output image is not necessarily the same as the size of the input image. Usually the result is taller and wider than the original. To determine how big the output image should be, you need to compute the dimensions of the rotated input area.

 
< previous page page_217 next page >